# Packages ----------------------------------------------------------------

if(!require(pacman)){install.packages("pacman")}
pacman::p_load(tidyverse, scales, broom)

# Data --------------------------------------------------------------------

data <- read_dta("data/adopted_methods_public.dta")
data <- filter(data, view != "MiM") %>% 
  mutate(
    view = ifelse(view == "SBS", "IDM", "SDM")
  ) %>% 
  # Fix client with wrong ndm
  mutate(
    ndm   = if_else(client_id_nopii == 634, 1, ndm),
    trmd0 = if_else(client_id_nopii == 634, 0, trmd0),
    trmd1 = if_else(client_id_nopii == 634, 1, trmd1),
  ) 

# Distribution of number of methods discussed -----------------------------

data_long <- data %>% 
  select(client_id_nopii, starts_with("trmd"), view, ndm) %>% 
  mutate(
    trmd35    = (trmd3 == 1 | trmd4 == 1 | trmd5 == 1),
    d0        = trmd0,
    # Discussed only 1 method
    d1trm1    = if_else(ndm == 1 &  trmd1  == 1, 1, 0),
    d1trm2    = if_else(ndm == 1 &  trmd2  == 1, 1, 0),
    d1trmdlow = if_else(ndm == 1 &  trmd35 == 1, 1, 0),
    # Discussed 2 methods
    d2trm12   = if_else(trmd1  == 1  & trmd2  == 1 & ndm == 2, 1, 0),
    d2trm1low = if_else(trmd1  == 1  & trmd35 == 1 & ndm == 2, 1, 0),
    d2trm2low = if_else(trmd2  == 1  & trmd35 == 1 & ndm == 2, 1, 0),
    d2trmlow  = if_else(trmd35 == 1  & trmd1  != 1 & trmd2 != 1 & ndm == 2 , 1, 0),
    # Discussed 3 methods or more
    d3else    = if_else(ndm >= 3, 1, 0)
  ) %>% 
  select(client_id_nopii, starts_with("d"), view) %>% 
  pivot_longer(
    -c(client_id_nopii, view)
  ) %>% 
  group_by(name, view) %>% 
  summarize(
    total = sum(value),
    mean = mean(value)
  ) %>% 
  arrange(view) %>% 
  ungroup() %>% 
  mutate(
    name = case_when(
      name == "d0"        ~ "Discussed\nno method",
      name == "d1trm1"    ~ "Top-ranked",
      name == "d1trm2"    ~ "2nd-ranked",
      name == "d1trmdlow" ~ "3rd, 4th or\n5th-ranked",
      name == "d2trm12"   ~ "Top- and\n2nd-ranked",
      name == "d2trm1low" ~ "Top- and 3rd- or\nlower-ranked",
      name == "d2trm2low" ~ "2nd- and 3rd- or\nlower-ranked",
      name == "d2trmlow"  ~ "3rd or lower",
      name == "d3else"    ~ "3+"
    ),
    name = fct_relevel(
      name, 
      "Discussed\nno method",
      "Top-ranked",
      "2nd-ranked",
      "3rd, 4th or\n5th-ranked",
      "Top- and\n2nd-ranked",
      "Top- and 3rd- or\nlower-ranked",
      "2nd- and 3rd- or\nlower-ranked",
      "3rd or lower",
      "3+"
    )
  ) %>%
  mutate(
    level = rep(c("", "Discussed 1 method", "Discussed 1 method", "Discussed 1 method", "Discussed 2 methods", "Discussed 2 methods", "Discussed 2 methods", "Discussed 2 methods", "Discussed 3 or\nmore methods"), times = 2),
    level = fct_relevel(
      level, "", "Discussed 1 method", "Discussed 2 methods", "Discussed 3 or\nmore methods"
    )
  ) 

data_long %>% 
  ggplot(
    aes(
      x = interaction(name,level),
      y = mean,
      fill = view
    )
  ) +
  geom_col(color = "black", position = "dodge") +
  geom_text(
    aes(
      label = paste0("N=",total, "\n", round(mean * 100,1), "%")
    ),
    position = position_dodge2(width = .9),
    vjust = -0.2,
    size = 3.5
  ) +
  geom_hline(yintercept = 0) +
  annotate(
    "text", x = c(1, 2, 3, 4, 5, 6, 7, 8, 9), y = -0.04,
    label = levels(factor(data_long$name)), size = 3
  ) +
  annotate(
    "text", x = c(1, 3, 6.5, 9), y = -0.15,
    label = levels(factor((data_long$level))), size = 3
  ) +
  annotate(
    "segment", x = 2, xend = 4, y = -0.1, yend = -0.1
  ) +
  annotate(
    "segment", x = 5, xend = 8, y = -0.1, yend = -0.1
  ) +
  annotate(
    "segment", x = 2, xend = 2, y = -0.1, yend = -0.08
  ) +
  annotate(
    "segment", x = 4, xend = 4, y = -0.1, yend = -0.08
  ) +
  annotate(
    "segment", x = 5, xend = 5, y = -0.1, yend = -0.08
  ) +
  annotate(
    "segment", x = 8, xend = 8, y = -0.1, yend = -0.08
  ) +
  labs(
    x = NULL,
    y = "",
    title = "Methods discussed in detail by ranking",
    fill = NULL
  ) +
  # scale_x_discrete(labels = wrap_format(15)) + 
  scale_y_continuous(labels = percent, limits = c(-0.15,0.8)) +
  scale_fill_manual(values = met.brewer("Kandinsky", 3)[2:3]) +
  theme_classic() +
  theme(
    legend.position = "bottom",
    axis.title.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank(),
    axis.line = element_blank()
  )

ggsave("output/figures/FigS6-view_trm_decomp.png", dpi = 320, height = 14, width = 20, units = "cm")
